#include "groupmodel.hpp"
#include "db.h"
// 创建群组
bool GroupModel::createGroup(Group &group)
{
    char sql[1024];
    sprintf(sql, "insert into AllGroup(groupname,groupdesc) values('%s','%s')",
            group.getName().c_str(), group.getDesc().c_str());
    MySQL mysql;
    if (mysql.connect())
    {
        if (mysql.update(sql))
        {
            group.setId(mysql_insert_id(mysql.getConn()));
            return true;
        }
    }
    return false;
}
// 加入群组
void GroupModel::addGroup(int userid, int groupid, string role)
{
    char sql[1024];
    sprintf(sql, "insert into GroupUser values(%d,%d,'%s')",
            groupid, userid, role.c_str());
    MySQL mysql;
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}
// 查询用户所在的群组信息（用于用户登入时，返回其加入的群组）
vector<Group> GroupModel::queryGroups(int userid)
{
    vector<Group> groupVec;
    char sql[1024] = {0};
    sprintf(sql,
            "select id,groupname,groupdesc from AllGroup ag inner join \
            GroupUser gu on gu.groupid=ag.id where gu.userid=%d",
            userid);
    MySQL mysql;
    if (mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                // 将查询出的所有群组信息存入vec中返回
                Group group;
                group.setId(atoi(row[0]));
                group.setName(row[1]);
                group.setDesc(row[2]);
                groupVec.push_back(group);
            }
            mysql_free_result(res);
        }
        // 为查询到的所有群组中的 成员字段 添加信息
        for (Group &group : groupVec)
        {
            sprintf(sql,
                    "select u.id,u.name,u.state,gu.grouprole from GroupUser gu  inner join \
            User u on u.id=gu.userid where gu.groupid=%d",
                    group.getId());
            MYSQL_RES *res = mysql.query(sql);
            if (res != nullptr)
            {
                MYSQL_ROW row;
                while ((row = mysql_fetch_row(res)) != nullptr)
                {
                    GroupUser user;
                    user.setId(atoi(row[0]));
                    user.setName(row[1]);
                    user.setState(row[2]);
                    user.setRole(row[3]);
                    group.getUsers().push_back(user);
                }
            }
            mysql_free_result(res);
        }
    }
    return groupVec;
}
// 查询群组内成员id，除用户自己，打包返回。用于服务器推送消息至所有人
vector<int> GroupModel::queryGroupUsers(int userid, int groupid)
{
    vector<int> idVec;
    char sql[1024] = {0};
    sprintf(sql,
            "select userid from GroupUser where groupid=%d and userid != %d",
            groupid, userid);
    MySQL mysql;
    if (mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if (res != nullptr)
        {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr)
            {
                // 将群组内的成员id（除自己外）全部放入idVec中
                idVec.push_back(atoi(row[0]));
            }
            mysql_free_result(res);
        }
    }
    return idVec;
}
